package com.aqielife.hk.controller;

import com.aqielife.hk.common.entity.ApiResult;
import com.aqielife.hk.dto.LoginDTO;
import com.aqielife.hk.service.IHcnetService;
import com.aqielife.hk.service.IUserService;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/hc")
public class HcController {

    private int lUserID =-1;
    @Autowired
    private IHcnetService iHcnetService;
    @Autowired
    private IUserService iUserService;

    private Logger logger = LoggerFactory.getLogger(HcController.class);
    /**
     * 登陆
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login")
    public String hcLogin() {
        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setName("admin");
        loginDTO.setIp("172.16.14.124");
        loginDTO.setPort((short) 8000);
        loginDTO.setPassword("gh123456");
        lUserID = iHcnetService.login(loginDTO);
        logger.info("luserID:"+lUserID);
        if (lUserID!=-1) {
            return ApiResult.Ok("注册成功！").toJSon();
        }
        return ApiResult.Error(500, "注册失败！").toJSon();
    }

    @ResponseBody
    @RequestMapping(value = "/userList")
    public String userList(String emo) throws JSONException {
        logger.info("luserID:"+lUserID);
        iUserService.searchUserInfo(lUserID,emo);
        return ApiResult.Ok("用户列表").toJSon();
    }

    @ResponseBody
    @RequestMapping(value = "/addUser")
    public String addUser(String emo) throws Exception {
        logger.info("luserID:"+lUserID);
        iUserService.addUserInfo(lUserID,emo);
        return ApiResult.Ok("用户列表").toJSon();
    }
}
